package dk.au.cs.bdsi.sms.resources;

import static dk.au.cs.bdsi.sms.representation.Representation.MEDIA_TYPE;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import dk.au.cs.bdsi.core.activities.DeleteException;
import dk.au.cs.bdsi.core.activities.UpdateException;
import dk.au.cs.bdsi.core.representation.InvalidRepresentationException;
import dk.au.cs.bdsi.core.resources.CoreResource;
import dk.au.cs.bdsi.sms.activities.CreateSmsActivity;
import dk.au.cs.bdsi.sms.activities.DeleteSmsActivity;
import dk.au.cs.bdsi.sms.activities.NoSuchSmsException;
import dk.au.cs.bdsi.sms.activities.ReadSmsActivity;
import dk.au.cs.bdsi.sms.activities.UpdateSmsActivity;
import dk.au.cs.bdsi.sms.domain.Sms;
import dk.au.cs.bdsi.sms.representation.SmsRepresentation;

/**
 * Sms resource, with which clients can interact to run the application protocol
 */
@Path("/sms")
public class SmsResource extends CoreResource {
	@POST
	@Path("/")
	@Consumes(MEDIA_TYPE)
	public Response createSms(String smsRepresentation) {
		try {
			Sms sms = SmsRepresentation.fromXml(smsRepresentation).getSms();
			SmsRepresentation response = new CreateSmsActivity().create(sms, requestUri());
			return Response.created(response.getSelfLink().getUri()).entity(response).build();
		} catch (InvalidRepresentationException e) {
			return Response.status(Status.BAD_REQUEST).build();
		} catch (Exception e) {
			return Response.serverError().build();
		}
	}
	
	@GET
	@Path("/{smsId}")
	@Produces(MEDIA_TYPE)
	public Response getSms() {
		try {
			SmsRepresentation response = new ReadSmsActivity().retrieve(requestUri());
			return Response.ok().entity(response).build();
		} catch (NoSuchSmsException e) {
			return Response.status(Status.NOT_FOUND).build();
		} catch (Exception e) {
			return Response.serverError().build();
		}
	}
	
	@POST
	@Path("/{smsId}")
	@Consumes(MEDIA_TYPE)
	@Produces(MEDIA_TYPE)
	public Response updateSms(String smsRepresentation) {
		try {
			Sms sms = SmsRepresentation.fromXml(smsRepresentation).getSms();
			SmsRepresentation response = new UpdateSmsActivity().update(sms, requestUri());
			return Response.ok().entity(response).build();
		} catch (InvalidRepresentationException e) {
			return Response.status(Status.BAD_REQUEST).build();
		} catch (NoSuchSmsException e) {
			return Response.status(Status.NOT_FOUND).build();
		} catch (UpdateException e) {
			return Response.status(Status.CONFLICT).build();
		} catch (Exception e) {
			return Response.serverError().build();
		}
	}
	
	@DELETE
	@Path("/{smsId}")
	@Produces(MEDIA_TYPE)
	public Response deleteSms() {
		try {
			SmsRepresentation response = new DeleteSmsActivity().delete(requestUri());
			return Response.ok().entity(response).build();
		} catch (NoSuchSmsException e) {
			return Response.status(Status.NOT_FOUND).build();
		} catch (DeleteException e) {
			return Response.status(405).header("Allow", "GET").build();
		} catch (Exception e) {
			return Response.serverError().build();
		}
	}
}
